/**
 * Created by John on 2016/7/14.
 */
var MongoClient = require('mongodb').MongoClient;
var mongo = new MongoClient();
var myDB = null;

mongo.connect('mongodb://localhost/', function (err, db) {
    myDB = db.db('BeginningMongoDB');
    myDB.collection('word_stats', function (err, collection) {
        sizesOfAllWords(collection);
        sizesOfWords(collection);
        firstLetterOfLongWords(collection);
        close();
    });
});

function sizesOfAllWords(collection) {
    collection.distinct('size', function (err, items) {
        console.log();
        console.log('Distinct Sizes of words:');
        console.log(items);
    })
}

function sizesOfWords(collection) {
    var query = {first: 'a'};
    collection.distinct('size', query, function (err, items) {
        console.log();
        console.log('Distinct Sizes of words starting with A:');
        console.log(items);
    })
}

function firstLetterOfLongWords(collection) {
    var query = {size: {$gt: 12}};
    collection.distinct('size', query, function (err, items) {
        console.log();
        console.log('Distinct first letters of words longer than 12 characters:');
        console.log(items);
    })
}

function close() {
    setTimeout(function () {
        myDB.close();
    }, 3000);
}